package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.PortEntity;
import com.bsg.upm.entity.SiteEntity;

/**
 * 端口管理业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class PortCheck extends BaseCheck {

	/**
	 * 端口新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 端口启用时检查
	 * 
	 * @param ports
	 *            端口集合
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkEnable(List<PortEntity> ports) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (ports.size() == 0) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg("端口不存在");
		}
		// for (PortEntity port : ports) {
		// if (port.getEnabled()) {
		// chkRS.setStatus(HttpStatus.SC_CONFLICT);
		// chkRS.setErrorMsg("端口：" + port.getPortValue() + "已是启用状态");
		// return chkRS;
		// }
		// }
		return chkRS;
	}

	/**
	 * 端口停用时检查
	 * 
	 * @param ports
	 *            端口集合
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkDisable(List<PortEntity> ports) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (ports.size() == 0) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg("端口不存在");
		}

		for (PortEntity port : ports) {
			// if (!port.getEnabled()) {
			// chkRS.setStatus(HttpStatus.SC_GONE);
			// chkRS.setErrorMsg("端口：" + port.getPortValue() + "已是停用状态");
			// return chkRS;
			// }

			if (port.isUsed()) {
				chkRS.setStatus(HttpStatus.SC_CONFLICT);
				chkRS.setErrorMsg("端口：" + port.getPortValue() + "已被使用，无法停用");
				return chkRS;
			}
		}
		return chkRS;
	}

	/**
	 * 端口删除时检查
	 * 
	 * @param ports
	 *            端口集合
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(List<PortEntity> ports) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (ports.size() == 0) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg("端口不存在");
		}
		for (PortEntity port : ports) {
			if (port.getEnabled()) {
				chkRS.setStatus(HttpStatus.SC_CONFLICT);
				chkRS.setErrorMsg("端口：" + port.getPortValue() + "是启用状态，无法删除");
				return chkRS;
			}
			if (port.isUsed()) {
				chkRS.setStatus(HttpStatus.SC_CONFLICT);
				chkRS.setErrorMsg("端口：" + port.getPortValue() + "已被使用，无法删除");
				return chkRS;
			}
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 站点非空检查
		String siteId = (String) paramMap.get("site");
		if (StringUtils.isBlank(siteId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "所属站点"));
			return chkRS;
		}

		// 起始端口非空和有效性检查
		Integer startPort = (Integer) paramMap.get("startPort");
		if (startPort == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "起始端口"));
			return chkRS;
		}
		if (startPort.intValue() < 10000 || startPort.intValue() > 65535) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "起始端口", 10000, 65535));
			return chkRS;
		}

		// 结束端口非空和有效性检查
		Integer endPort = (Integer) paramMap.get("endPort");
		if (endPort == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "结束端口"));
			return chkRS;
		}
		if (endPort.intValue() < 10000 || endPort.intValue() > 65535) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "结束端口", 10000, 65535));
			return chkRS;
		}
		if (endPort.intValue() < startPort.intValue()) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("起始端口不能大于结束端口");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 站点存在性检查
		String siteId = (String) paramMap.get("site");
		SiteEntity site = siteDao.get(siteId);
		if (site == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "所属站点"));
			return chkRS;
		}

		// 端口重复性检查
		Integer startPort = (Integer) paramMap.get("startPort");
		Integer endPort = (Integer) paramMap.get("endPort");
		int portCount = portDao.countPort(siteId, startPort, endPort);
		if (portCount > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "站点下端口范围"));
			return chkRS;
		}
		return chkRS;
	}

}
